<!-- 收文办理列表 -->
<template>
  <view>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="load_more"
                offset="20" :error.sync="error" error-text="请求失败，点击重新加载"
                :immediate-check="immediateCheck">
        <van-cell center v-for="item in list" :title="item.title"
                  :label="'公文状态：' + (item.swState==null?'':item.swState)"
                  @click="toDoReceive(item)"  value-class="text-sm">
          <template #title>
            <span>{{item.title}}</span>
          </template>
          <template #extra>
            <span class="padding-left-sm">{{dateFormat(item.updateTime)}}</span>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </view>
</template>

<script>
	
	export default {
		props:{
			swType: {
				type: String
			},
			paramObj: {
				type: Object
			},
			finishStatus: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				list: [],
				loading: false,   // 加载状态
				finished: false,  // 是否加载
				refreshing: false,// 是否刷新
				error: false,     // 错误状态
				pageNum: 1,   		// 页数
				pageSize: 10, 		// 条数
				receType: '', 		// 收文类型
				immediateCheck: true, // 是否在初始化时立即执行滚动位置检查　　　
			}
		},
		
		created() {
			// console.log('------------created()');
			// this.onRefresh()
		},
		
		watch: {
			swType: {
				handler(nval, oval) {
					this.receType = nval
					this.onRefresh()
				},
				immediate: true,
				deep: true
			},
			finishStatus: {
				handler(nval, oval) {
					this.finished = nval
				},
				immediate: true,
				deep: true
			}
		},
		
		methods: {
			// 下拉刷新
			onRefresh() {
				console.log('----------------onRefresh-->> ' + this.receType);
				// 先清空数据
				this.list = []
				// 将没有更多的状态改成false
				this.finished = false
				// 默认加载第一页
				this.pageNum = 1
				this.getList()			
				this.refreshing = false
			},
			
			// 上拉加载
			load_more() {
				console.log('----------------load_more-->> ' + this.receType);
				// 页数设置
				if( this.list.length == 0){
					this.pageNum = 1
				} else {  
					this.pageNum++
				}
				// 将下拉刷新停止
				this.refreshing = false;
				setTimeout(() => {
					this.getList()
				}, 500)
			},
			
			// 获取收文数据
			getList() {
				console.log('----------------getList-->> ' + this.receType);
				// 加载状态
				this.loading = true
				this.$http({
				  url: this.$http.adornUrl(`/caac/oa/doc/swbanli/swbanlilist`),
				  method: 'post',
				  params: this.$http.adornParams({
					  'systemKey': this.cache.get('userInfo').systemKey,  
					  'systemFlag': this.cache.get('userInfo').systemFlag,
						'page': this.pageNum,
						'rows': this.pageSize,
						'isJldps': this.receType,
						'swWord': ""
				  })
				}).then(({data}) => {
					if(data.code == 200) {
						if(data.data != null && JSON.stringify(data.data) != '{}') {
							if(this.pageNum == 1) {
								this.list = data.data.rows
							} else {
								data.data.rows.forEach( t => this.list.push(t))								
							}
							
							// 判断当前页是否为最后一页
							if((this.pageNum*this.pageSize) > data.data.total) {
								this.finished = true
							}
						} else {
              this.finished = true
            }
					} else {
						this.error = true
					}
				}).catch((err) => {
					this.error = true
				}).finally(() => {
					// 关闭加载状态
					this.loading = false
				})
				
			},
			
			// 时间格式化
			dateFormat(dateTime) {
				let formatStr = dateTime
				if(dateTime != null && dateTime != '' && dateTime.length > 10) {
					formatStr = dateTime.substr(0, 10)
				}
				return formatStr
			},
			
			// 跳转收文详情
			toDoReceive(item) {
				console.log("----------------item: " + JSON.stringify(item));
				uni.navigateTo({
					url:"/pages/jgj/receive/index?proInstId=" + item.proInstId  + "&from=swbl"
				})
			},
		},
		
		
		
	}
</script>

<style>
	.van-cell__title {
		min-width: 70%;
	
		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}
</style>
